package CutLoad.components
{
	import CutLoad.Base;
	import CutLoad.components.container.ContainerV;
	
	import flash.events.MouseEvent;

	/**
	 * 列表 
	 * @author smallerbird mail:smallerbird@gmail.com  site:www.mkigm.com QQ群:49983196
	 * 
	 */	
	public class CList extends Base
	{
		private var arrData:Array;
		private var container:ContainerV;
		private var funGetOne:Function;
		private var arrItem:Array;
		private var FunMouseClickItem:Function;
		private var FunMouseOverItem:Function;
		public function CList(arrData:Array=null,spaceY:Number=3,funGetOne:Function=null)
		{
			arrItem=new Array();
			if(arrData==null) arrData=new Array();
			this.arrData=arrData
			container=new ContainerV();
			container.init({w:0,h:0,isAuto:false,xspace:spaceY,yspace:spaceY});
			container.show(this);
			if(funGetOne==null) this.funGetOne=default_funGetOne;
			//检测
			if(funGetOne!=null){
				try{
					funGetOne("","");
				}catch(e:ArgumentError){
					throw new Error("funGetOne 这个参数的函数要有两个参数！");
				}
			}
			if(arrData!=null){
				appendSome(arrData);
			}
			
		}
		/**
		 * 设置鼠标点击某一项的回调函数 
		 * @param fun 格式 function(id,label,data)
		 * 
		 */		
		public function setFunMouseClickItem(fun:Function):void{
			FunMouseClickItem=fun;
		}
		/**
		 * 设置鼠标放上去某一项的回调函数 
		 * @param fun 格式 function(id,label,data)
		 * 
		 */		
		public function setFunMouseOverItem(fun:Function):void{
			FunMouseOverItem=fun;
		}
		/**
		 * 添加一个元素 
		 * @param label
		 * @param data
		 * 
		 */		
		public function append(label:String,data:String):void{
			var baseTem:Base=funGetOne(label,data);
			container.append(baseTem)
			arrItem.push(baseTem);
			setMouseEvent(baseTem,arrItem.length-1,label,data);
		}
		/**
		 * 添加一组元素 
		 * @param arrData  二维数组，每一个元素  [label,data]
		 * 
		 */		
		public function appendSome(arrData:Array):void{
			var arrTem:Array=new Array();
			for(var i:Number=0;i<arrData.length;i++){
				var baseTem:Base=funGetOne(arrData[i][0],arrData[i][1]);
				arrTem.push(baseTem);
				arrItem.push(baseTem);
				setMouseEvent(baseTem,arrItem.length,arrData[i][0],arrData[i][1]);
			}
			container.appendSome(arrTem);
		}
		/**
		 * 获得单个元素
		 * @param index
		 * @return 
		 * 
		 */		
		public function getItem(id:Number):Base{
			if(id<0||id>arrItem.length-1) return null;
			return arrItem[id] as Base
		}
		/**
		 * 获得单个元素的值 
		 * @param id
		 * @return [id,label,data]
		 * 
		 */		
		public function getItemValue(id:Number):Array{
			var item:Base=arrItem[id] as Base;
			return item.getOtherInfo().arr;
		}
		private function setMouseEvent(base:Base,id:Number,label:String,data:String):void{
			base.addEventListener(MouseEvent.CLICK,mouseClick);
			base.addEventListener(MouseEvent.MOUSE_OVER,mouseOver);
			//
			base.setOtherInfo({arr:[id,label,data]});
		}
		private function mouseClick(e:MouseEvent):void{
			var baseTem:Base=e.target as Base;
			var arr:Array=baseTem.getOtherInfo().arr;
			FunMouseClickItem(arr[0],arr[1],arr[2]);
		}
		private function mouseOver(e:MouseEvent):void{
			var baseTem:Base=e.target as Base;
			var arr:Array=baseTem.getOtherInfo().arr;
			FunMouseOverItem(arr[0],arr[1],arr[2]);
		}
		/**
		 * 默认生成的的元素 
		 * @param label
		 * @param data
		 * @return 
		 * 
		 */		
		private function default_funGetOne(label:String,data:String):Base{
			var btn:CButton=new CButton();
			btn.init(label,false,true,true,true);
			return btn;
		}
		/**
		 * 设置宽度，注：这里设置高度无效 
		 * @param w 
		 * @param h
		 * 
		 */		
		override public function setWH(w:Number=0, h:Number=0):void{
			for(var i:Number=0;i<arrItem.length;i++){
				var BaseItem:Base=arrItem[i] as Base;
				BaseItem.setWH(w,0);
			}
			container.setWH(w,0);
		}
		override public function killMe():void{
			for(var i:Number=0;i<arrItem.length;i++){
				var BaseItem:Base=arrItem[i] as Base;
				BaseItem.removeEventListener(MouseEvent.CLICK,mouseClick);
				BaseItem.removeEventListener(MouseEvent.MOUSE_OVER,mouseOver);
			}
			super.killMe();
		}
	}
}